"""
Case Type   : mot
Case Name   : gs_guc reload方式设置enable_mot_server参数
Create At   : 2025/3/26
Owner       : @li-xin12345
Description :
    1、reload修改参数值为on
    2、重启，查看参数设置是否生效
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class MotTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.param_name = 'enable_mot_server'
        self.default_value = self.common.show_param(self.param_name)

    def test_mot(self):
        text = '-----step1:reload修改参数值为on;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execute_gsguc('reload',
                                        self.constant.GSGUC_SUCCESS_MSG,
                                        f'{self.param_name}=on')
        self.log.info(res)
        self.assertTrue(res, "执行gs_guc set失败，预期成功但未找到标志信息")

        res = self.common.show_param(self.param_name)
        self.log.info(res)
        self.assertEqual('off', res, '执行失败：' + text)

        text = '-----step2:重启，查看参数设置是否生效;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.restart_db_cluster()
        self.log.info(res)
        status = self.pri_sh.get_db_cluster_status()
        self.assertTrue("Normal" or "Degrade" in status)

        res = self.common.show_param(self.param_name)
        self.log.info(res)
        self.assertEqual('on', res, '执行失败：' + text)

    def tearDown(self):
        text = '-----step3:清理环境; expect:成功-----'
        self.log.info(text)
        res = self.common.show_param(self.param_name)
        self.log.info(res)
        if res != self.default_value:
            set_res = self.pri_sh.execute_gsguc(
                'set', self.constant.GSGUC_SUCCESS_MSG,
                f'{self.param_name}={self.default_value}')
            self.log.info(set_res)
            res = self.pri_sh.restart_db_cluster()
            self.log.info(res)
            status = self.pri_sh.get_db_cluster_status()
            self.assertTrue(set_res, "执行gs_guc set失败，预期成功但未找到标志信息")
            self.assertTrue("Normal" or "Degrade" in status)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')